//
//  ComViewController.swift
//  TBCode
//
//  Created by nexgo on 2017/10/31.
//  Copyright © 2017年 truth. All rights reserved.
//

import UIKit

class ComViewController: UIViewController {
    
    class func getActivity() -> ComViewController{
        let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main);
        let vc = storyBoard.instantiateViewController(withIdentifier: "ComViewController") as! ComViewController;
        
        return vc;
    }
    

    public var workDevice:TBBLEDevice!;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initBLE();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // 初始化蓝牙
    private var bleManager:TBBLEManager!;
    private var ble:TBBLEBase!;
    private var bleConnectCB:TBBLEBaseCB!;
    private func initBLE(){
        bleManager = TBBLEManager.getInstance();
        ble = TBBLEBase(bleManager);
        
        bleConnectCB = TBBLEBaseCB();
        
        bleConnectCB.onConnect = self.onConnect;
        bleConnectCB.onRetryConnect = self.onRetryConnect;
        bleConnectCB.onDisConnect = self.onDisConnect;
        bleConnectCB.onManualDisConnect = self.onManualDisConnect;
        bleConnectCB.onCharactChanged = self.onCharactChanged;
        
        conectDevice();
        
    }
    
    private func onConnect(errorCode:TBErrorCode?){
        NSLog("onConnect->\(errorCode?.msg!)");
        
        if(errorCode?.code != TBSUCCESS){
            print("连接失败");
            return;
        }
        
        sendMsg();
    }
    private func onRetryConnect(retryTimes:Int32){
        NSLog("onRetryConnect->\(retryTimes)");
    }
    private func onDisConnect(errorCode:TBErrorCode?){
        NSLog("onDisConnect->\(errorCode?.msg!)");
    }
    private func onManualDisConnect(errorCode:TBErrorCode?){
        NSLog("onManualDisConnect->\(errorCode?.msg)");
    }
    private func onCharactChanged(character: CBCharacteristic?, data:Data?){
        NSLog("onCharactChanged->\(data)");
    }
    
    private func conectDevice(){
        NSLog("开始连接");
        ble.connect(workDevice, retryTimes: 2, cb: bleConnectCB);
    }
    
    private func sendMsg(){
        
        var character = ble.getCharacterByUuid("0000FEE9-0000-1000-8000-00805F9B34FB", characterUuid: "D44BC439-ABFD-45A2-B575-925416129600");
        
        if(character == nil){
            print("character == nil");
            return;
        }
        
        "123456789".data(using: String.Encoding.utf8)
        
        let writeAction = TBBLEWriteCharacterAction(character, data: "12345678901234567890111111111122222222223333333333444444444455555".data(using: String.Encoding.utf8), option: TBBLEBaseOption())!;
        writeAction.cb = TBBLEWriteCharacterActionCB();
        writeAction.cb!.onCancel = self.onSendDatasCancel;
        writeAction.cb!.onSuccess = self.onSendDataSuccess;
        writeAction.cb!.onFail = self.onSendDataFail;
        writeAction.cb!.onCancel = self.onSendDataCancel;
        (writeAction.cb as! TBBLEWriteCharacterActionCB).onSendPerData = self.onSendDataSendPerData;
        
        ble.add(writeAction);
    }
    
    private func onSendDatasCancel(){
        print("onSendDatasCancel");
    }
    private func onSendDataSuccess(){
        print("onSendDataSuccess");
    }
    private func onSendDataFail(errorCode:TBErrorCode){
        print("onSendDataFail->\(errorCode.msg)");
    }
    private func onSendDataCancel(){
        print("onSendDatasCancel");
    }
    private func onSendDataSendPerData(data: Data){
        
        print("onSendDataSendPerData->\(Utils.data2HexStringWithSpace(data: data, separ: " "))");
    }
}
